package com.esiag.pdsteam.add;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;
import java.util.List;

public class ExistantsMembers extends Thread{
   InetAddress  groupeIP;
   int port;
   String nom;
   MulticastSocket socketReception;
   
   private List<String> list = new ArrayList<>();

   public List<String> getList() {
	return list;
}
public ExistantsMembers(InetAddress groupeIP, int port, String nom)  throws Exception { 
	   this.groupeIP = groupeIP;
	   this.port = port;
	   this.nom = nom;
	   socketReception = new MulticastSocket(port);
	   socketReception.joinGroup(groupeIP);
	   start();
	   
  }

public void run() {
    DatagramPacket adresseMember;
    byte[] contenuMessage;
    String adresse;
    
    while(true) {
		  contenuMessage = new byte[1024];
		  adresseMember = new DatagramPacket(contenuMessage, contenuMessage.length);
		  try {
	              socketReception.receive(adresseMember);
	              adresse = (new DataInputStream(new ByteArrayInputStream(contenuMessage))).readUTF();
	              list.add(adresse);
	       	   System.out.println(list);
		  }
		  catch(Exception exc) {
	    		System.out.println(exc);
		  }
    }
  }
}